
<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="库位管理">
            <span>所属仓库：</span>
            <el-select v-model="depValue" @change="selectDep" placeholder="请选择仓库" >
              <el-option
                v-for="(item,index) in depList"
                :key="item.id"
                :label="item.name"
                :value="index">
              </el-option>
            </el-select>
            <span  class="ml-10">所属分区：</span>
            <el-select v-model="parValue" @change="parChange" placeholder="请选择分区" >
              <el-option
                v-for="(item) in parList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
            <span  class="ml-10">所属货架：</span>
            <el-select v-model="sheValue"  placeholder="请选择货架" >
              <el-option
                v-for="(item) in sheList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
            <!-- <el-button type="primary" size="medium">
              请选择分区<i class="el-icon-arrow-down el-icon--right" />
            </el-button> -->
            <el-button
            class="ml-10"
              type="primary"
              size="medium"
              icon="el-icon-search"
              @click="search"
            >查询</el-button>
            <el-button type="primary" size="medium" @click="showDialog=true">
              <i class="el-icon-plus" />新建库位
            </el-button>
            <!-- 表格 -->
            <el-table :data="list" class="mt-20">
              <!-- <el-table-column prop="num" label="编号"  />
              <el-table-column prop="row" label="第几排" /> -->
              <el-table-column prop="finalLocation" label="最终的库位" />
              <el-table-column prop="internalOrderid" label="国内订单id" />

              <el-table-column prop="text" fixed="right"  label="操作" >
                <template slot-scope="scope">
                  <el-button size="small" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>

        </el-tabs>
      </el-card>

    </div>
    <!-- 分页 -->
    <div align="center">
      <el-pagination
        :current-page.sync="currentPage"
        layout="prev, pager, next, jumper"
        :total="total"
        :page-size="pageSize"
        @current-change="handleCurrentChange"
      />
    </div>
     <!-- 新增、修改弹窗 -->
    <el-dialog  :visible="showDialog" @close="closeDialog">
      <el-form label-width="110px">
      
        <el-form-item label="仓库/分区/货架" prop="num">
          <el-cascader :props="props" v-model="cascaderValue" placeholder="请选择仓库/分区/货架"></el-cascader>
        </el-form-item>
        <!-- <el-form-item label="编号" prop="num">
          <el-input v-model="formData.num" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="第几排" prop="row">
          <el-input v-model="formData.row" placeholder="第几排 " />
        </el-form-item> -->
        <el-form-item label="最终库位" prop="finalLocation">
          <el-input v-model="formData.finalLocation" placeholder="请输入最终库位 " />
        </el-form-item>
        <el-form-item label="国内订单号" prop="internalOrderid">
          <el-input v-model="formData.internalOrderid" placeholder="请输入国内订单号 " />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog=false">取消</el-button>
          <el-button type="primary" @click="editSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog
        title="提示"
        :visible.sync="deleteVisible"
        width="30%"
      >
        <span>确定删除吗？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="deleteVisible = false">取 消</el-button>
          <el-button type="primary" @click="deleteComfirm">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { selectAllDep,fpartitionByDepId,selectShelvesByFid ,selectLocationByDid,selectLocationByOrderId,selectLocationByFid,insertLocation,deleteLocation,selectLocationBySid,updateLocation} from '@/api/depository'

export default {
  name: 'Dashboard',
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  data() {
    return {
      cascaderValue:null,
      depValue:'',//所选仓库
      depList:[], //仓库列表
      parList:[],//分区列表
      parValue:'',//所选分区
      sheList:[],//货架列表
      sheValue:'',//所选货架
      currentPage: 1,
      list: [
      ],
      showDialog:false,
      deleteVisible:false,
      formData:{
        depositoryId:8,
        fpartitionId:19,
        shelvesId:5
      },
      deleteId:null,
      selectParList:[],
      pageSize:8,
      total:0,
      cascaderList:[],
      props: {
        lazy: true,
        async lazyLoad (node, resolve) {
          const { level,data } = node;
          if(level==0){
            const depList = await selectAllDep()
            depList.map((item)=>{
              item. value= item.id
              item.label=item.name
              return item
            })
            resolve(depList)
          }else if(level==1){
            const parList = await fpartitionByDepId({
              depId:node.data.id
            })
            parList.map((item)=>{
              item. value= item.id
              item.label=item.name
              return item
            })
            resolve(parList)
          }else{
            const sheList = await selectShelvesByFid({
              fpartitionId:node.data.id
            })
            sheList.map((item)=>{
              item. value= item.id
              item.label=item.name
              item.leaf=true
              return item
            })
            resolve(sheList)
          }
        }
      }
    }
  },
  mounted(){
    this.selectAllDep()
  },
  methods: {
    async selectAllDep(){
      let res = await selectAllDep()
      this.depList = res
      res.map((item)=>{
        item. value= item.id
        item.label=item.name
        item.children=[]
        return item
      })
      this.cascaderList = res
      // this.cascaderList.children=[]
    },
    async selectDep(){
      // this.getParList(this.depList[e].id)
      const parList = await fpartitionByDepId({
        depId:this.depList[this.depValue].id
      })
      this.parList = parList
      this.parValue = this.parList[this.depValue].id
      this.sheList = []
      this.sheValue=''
    },
    async parChange(){
      const sheList = await selectShelvesByFid({
        fpartitionId:this.parValue
      })
      this.sheList = sheList
      this.sheValue = sheList[0].id
    },
    async search(){
      let res = []
      if(this.sheValue){
         res = await selectLocationBySid({
          sId:this.sheValue,
          page:this.currentPage,
          num:this.pageSize
        })
      }else if(this.depValue){
         res = await selectLocationByFid({
          fId:this.parValue,
          page:this.currentPage,
          num:this.pageSize
        })
      }else{
         res = await selectLocationByDid({
          dId:this.depList[this.depValue].id,
          page:this.currentPage,
          num:this.pageSize
        })
      }
      this.list=res.list
      this.total = res.total
      // if(res){
      //   res.map((item)=>{
      //     item.statename = item.state?'有效':'无效'
      //     item.storagename = item.storage?'未满':'已满'
      //     return item
      //   })
      //   this.list = res
      // }
      
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.search()
    },
    closeDialog(){
      this.showDialog = false
    },
    async editSubmit(){
      this.formData.depositoryId= this.cascaderValue[0]
      this.formData.fpartitionId= this.cascaderValue[1]
      this.formData.shelvesId= this.cascaderValue[2]
      if(this.formData.id){
        await updateLocation(this.formData)
      }else{
        console.log(this.formData)
        await insertLocation(this.formData)
      }
      this.showDialog=false;
      this.search()
    },
    handleDelete(row){
      this.deleteId=row.id
      this.deleteVisible=true
    },
    async deleteComfirm(){
      await deleteLocation({
        id:this.deleteId
      })
      this.$message.success('操作成功')
      this.search()
      this.deleteVisible = false
    },
    handleEdit(row){
      this.formData = row
      this.showDialog = true
      this.cascaderValue=[this.depValue,this.parValue,this.sheValue]
    },
    async selectDep2(){
      const res = this.selectParList = await fpartitionByDepId({
        depId:this.formData.depositoryid
      })
      if(res.length){
        this.formData.fpartitionId = this.selectParList[0].id
      }
      
    },
    cascaderChange(){
      console.log(this.cascaderValue)
    }
  },
 

}
</script>

<style lang="scss" scoped>
  .xinzeng{
    width: 70px;
    height:40px;
    float:right;
    margin-top: -55px;
    margin-left: 210px
  }
  .el-dropdown {
    vertical-align: top;
  }
  .el-dropdown + .el-dropdown {
    margin-left: 15px;
  }
  .el-icon-arrow-down {
    font-size: 10px;
  }
  .fenqu{
        width: 180px;
        height:40px;
        float: right;
        margin-top: -70px;
        margin-right: 550px;
  }
  .del{
    width: 50px;

  }
</style>

